package com.ldu.houserent.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldu.houserent.common.base.BaseController;
import com.ldu.houserent.common.util.PageUtil;
import com.ldu.houserent.entity.News;
import com.ldu.houserent.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 前端新闻资讯的控制器
 */
@Controller("frontNewsController")
@RequestMapping("/news")     //新闻资讯页面的地址
public class NewsController extends BaseController {

    @Autowired
    private NewsService newsService;

    /*
        进入新闻资讯列表页面
     */
    @RequestMapping("")             //此处""内的内容是拼接在/news之后的内容
    public String newsList(@RequestParam(value = "page",defaultValue = "1")Long pageNumber, @RequestParam(value = "size",defaultValue = "6")Long pageSize, Model model){
        //封装page
        Page page= PageUtil.initMpPage(pageNumber,pageSize);
        //进行查询
        News condition = new News();
        Page<News> newsPage = newsService.findAll(page,condition);     //condition是查询条件
        model.addAttribute("pageInfo",newsPage);
        model.addAttribute("pagePrefix","/news?");
        return "front/news-list";      //指向jsp页面
    }

    /*
        新闻详情
     */
    @RequestMapping("/detail/{id}")             //此处""内的内容是拼接在/news之后的内容
    public String publish(@PathVariable(value = "id",required = false)Long id, Model model){     //用model将当前数据传给前端
        News news=newsService.get(id);
        if (news==null){
            return renderNotFound();        //转向404页面
        }
        model.addAttribute("news",news);
        return "front/news-detail";      //指向jsp页面
    }

}//NewsController
